import os
import shutil
import re

DIR = "f:\\doc"

def change_img_src(filename, parent_path):
    print filename
    f = open(filename)
    data = f.read()
    f.close()
    lst = re.findall("src=\"(.+?)(jpg)?(png)?\"", data, re.S)
    for i in lst:
        if len(i) == 2:
            data = data.replace(i[0], "doc/" + parent_path + "/" + i[0])
    f = open(filename, "w")
    f.write(data)
    f.close()

for root, dirs, files in os.walk(DIR):  
    for name in files:
        if name.endswith(".htm"):
            change_img_src( os.path.join(root, name), root.split("\\")[-1] )
